package com.xinpu.vo;

import java.io.Serializable;

public class ResponseMessage<T> implements Serializable {

    public enum ErrorCode {
        Success(200),
        Error(300),
        TaoBaoUnAuthorized(700),
        UnAuthorized(1000);

        private ErrorCode(int val) {
            this.value = val;
        }

        private int value;

        public int getValue() {
            return value;
        }
    }

    /**
     * 消息
     */
    protected String message;

    protected T result;

    protected int status;

    /**
     * 业务码
     */
    protected int code;

    private Long timestamp;


    public String getMessage() {
        return message;
    }


    public T getResult() {
        return result;
    }


    public int getStatus() {
        return status;
    }


    public Long getTimestamp() {
        return timestamp;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public ResponseMessage() {

    }

    /**
     * 设置当前的时间戳
     *
     * @return
     */
    public ResponseMessage<T> putTimeStamp() {
        this.timestamp = System.currentTimeMillis();
        return this;
    }

    /**
     * 消息
     *
     * @param msg
     * @return
     */
    public ResponseMessage<T> message(String msg) {
        this.message = msg;
        return this;
    }

    public ResponseMessage<T> code(int code) {
        this.code = code;
        return this;
    }

    /**
     * 状态码
     *
     * @param status
     * @return
     */
    public ResponseMessage<T> status(int status) {
        this.status = status;
        return this;
    }

    /**
     * 设置结果
     *
     * @param result
     * @return
     */
    public ResponseMessage<T> result(T result) {
        this.result = result;
        return this;
    }

    /**
     * 错误消息
     *
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseMessage<T> error(String message) {
        return error(ErrorCode.Error.getValue(), message);
    }


    /**
     * 错误消息
     *
     * @param message
     * @param result
     * @param <T>
     * @return
     */
    public static <T> ResponseMessage<T> error(String message, T result) {
        return error(ErrorCode.Error.getValue(), message, result);
    }

    /**
     * 错误消息
     *
     * @param message
     * @param code
     * @param <T>
     * @return
     */
    public static <T> ResponseMessage<T> error(String message, int code) {
        return error(ErrorCode.Error.getValue(), message, null, code);
    }

    /**
     * 授权失败
     *
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseMessage<T> unautho(String message) {
        return error(ErrorCode.UnAuthorized.getValue(), message);
    }

    /**
     * 异常处理
     *
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseMessage<T> exception(String message) {
        return error(ErrorCode.Error.getValue(), message);
    }

    /**
     * 错误消息
     *
     * @param status
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseMessage<T> error(int status, String message) {
        ResponseMessage<T> msg = new ResponseMessage<>();
        msg.message = message;
        msg.status = status;
        msg.putTimeStamp();
        return msg;
    }

    /**
     * 错误消息
     *
     * @param status
     * @param message
     * @param result
     * @param <T>
     * @return
     */
    public static <T> ResponseMessage<T> error(int status, String message, T result) {
        ResponseMessage msg = error(status, message);
        msg.result(result);
        return msg;
    }

    public static <T> ResponseMessage<T> error(int status, String message, T result, int code) {
        ResponseMessage msg = error(status, message);
        msg.result(result);
        msg.code(code);
        return msg;
    }

    /**
     * 返回正确的数据
     *
     * @param <T>
     * @return
     */
    public static <T> ResponseMessage<T> ok() {
        return ok(null);
    }


    /**
     * 返回正确的数据
     *
     * @param result
     * @param <T>
     * @return
     */
    public static <T> ResponseMessage<T> ok(T result) {
        return ok(result, 0);
    }

    public static <T> ResponseMessage<T> ok(T result, int code) {
        return (new ResponseMessage<T>()).message("success").result(result).putTimeStamp().status(ErrorCode.Success.getValue()).code(code);
    }

    /**
     * 判断是否为成功
     *
     * @return
     */
    public boolean IsSuccess() {
        return this.status == ErrorCode.Success.getValue();
    }
}

